<template>
  <el-form inline="true">
    <el-form-item>
      <selector v-model="params.difficulty" placeholder="请选择题目难度" style="width: 200px;"></selector>
    </el-form-item>
    <el-form-item>
      <el-input v-model="params.title" placeholder="请您输入要搜索的题目标题"/>
    </el-form-item>
    <el-form-item>
      <el-button plain @Click="onSearch">搜索</el-button>
      <el-button plain type="info" @click="onReset">重置</el-button>
      <el-button plain type="primary" :icon="Plus" @click="onAddQuestion">添加题目</el-button>
    </el-form-item>
  </el-form>
  <el-table height="526px" :data="questionList">
    <el-table-column prop="questionId" width="180px" label="题目id"/>
    <el-table-column prop="title" label="题目标题"/>
    <el-table-column prop="difficulty" label="题目难度" width="90px">
      <template #default="{ row }">
        <div v-if="row.difficulty === 1" style="color:#3EC8FF;">简单</div>
        <div v-if="row.difficulty === 2" style="color:#FE7909;">中等</div>
        <div v-if="row.difficulty === 3" style="color:#FD4C40;">困难</div>
      </template>
    </el-table-column>
    <el-table-column prop="createName" label="创建人" width="140px"/>
    <el-table-column prop="createTime" label="创建时间" width="180px"/>
    <el-table-column label="操作" width="100px" fixed="right">
      <template #default="{ row }">
        <el-button type="text" @click="onEdit(row.questionId)">编辑
        </el-button>
        <el-button type="text" class="red" @click="onDelete(row.questionId)">删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination background size="small" layout="total, sizes, prev, pager, next, jumper" :total="total"
                 v-model:current-page="params.pageNo" v-model:page-size="params.pageSize"
                 :page-sizes="[1, 5, 10, 15, 20]" @size-change="handleSizeChange"
                 @current-change="handleCurrentChange"/>

  <question-drawer ref="questionEditRef" @success="onSuccess"></question-drawer>

</template>

<script setup>
import {Plus} from "@element-plus/icons-vue"
import Selector from "@/components/QuestionSelector.vue"
import {reactive, ref} from "vue";
import {getQuestionListService} from "@/apis/question.js";
import {ElMessage, ElMessageBox} from "element-plus";
import QuestionDrawer from "@/components/QuestionDrawer.vue";
import {deleteQuestionService} from "@/apis/question.js";

const params = reactive({
  pageNo: 1,
  pageSize: 10,
  difficulty: '',
  title: ''
})

const questionList = ref([])
const total = ref(0)

// 静态数据
questionList.value = [
  {
    "questionId": 1,
    "title": "两数之和",
    "difficulty": 1,
    "createName": "管理员",
    "createTime": "2021-08-01 12:00:00",
  },
  {
    "questionId": 2,
    "title": "两数相加",
    "difficulty": 2,
    "createName": "管理员",
    "createTime": "2021-08-01 12:00:00",
  },
  {
    "questionId": 3,
    "title": "无重复字符的最长子串",
    "difficulty": 3,
    "createName": "管理员",
    "createTime": "2021-08-01 12:00:00",
  }
]


// 动态数据
async function getQuestionList() {
  const result = await getQuestionListService(params)
  if (result === undefined) {
    ElMessage.error('获取题目列表失败, 展示静态数据')
    return
  }

  // 解析响应数据
  const {data} = result
  questionList.value = data.list
  total.value = data.total
}

getQuestionList()

const onSearch = () => {
  console.log(params)
  // 重置当前页, 优化用户体验
  params.pageNo = 1
  getQuestionList()
}


const onReset = () => {
  params.difficulty = ''
  params.title = ''
  params.pageNo = 1
  params.pageSize = 10
  getQuestionList()
}

const questionEditRef = ref()
const onAddQuestion = async () => {
  //  添加题目
  // console.log('添加题目')
  await questionEditRef.value.open()
}

const onEdit = async (questionId) => {
  //  编辑题目
  // console.log('编辑题目', questionId)
  await questionEditRef.value.open(questionId)

}

function onSuccess(service) {
  if (service === 'add') {
    params.pageNo = 1
  }
  getQuestionList()
}

const onDelete = (questionId) => {
  //  删除题目
  // console.log('删除题目', questionId)
  // 询问用户是否删除
  ElMessageBox.confirm('此操作将永久删除该题目, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 确认删除
    deleteQuestionService(questionId).then(() => {
      ElMessage.success('删除成功')
      getQuestionList()
    }).catch(() => {
      ElMessage.error('删除失败')
    })
  }).catch(() => {
    // 取消删除
    ElMessage.info('已取消删除')
  })
}

const handleSizeChange = (val) => {
  // console.log(`每页 ${val} 条`)
  // 重置当前页, 优化用户体验
  params.pageNo = 1
  getQuestionList()
}

const handleCurrentChange = (val) => {
  // console.log(`当前页: ${val}`)
  getQuestionList()
}
</script>